<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Report_item_model extends MY_Model {

	const TABLE = 'report_item';

	public function add($item_sort, $item_key, $item_name, $item_type, $item_server, $group_id) {
		$data = array(
			'item_sort' => $item_sort,
			'item_key'  => $item_key,
			'item_name' => $item_name,
			'item_type' => $item_type,
			'item_server' => $item_server,
			'group_id'  => $group_id,
		);
		if(!is_null($group_id)) {
			$this->db->insert(self::TABLE, $data);
		}
		return $this->db->insert_id();
	}

	public function delete($item_id) {
		$this->db->where('item_id', $item_id);
		$this->db->delete(self::TABLE);
	}

	public function update($item_id, $item_sort, $item_key, $item_name, $item_type, $item_server, $group_id) {
		$data = array(
			'item_sort' => $item_sort,
			'item_key'  => $item_key,
			'item_name' => $item_name,
			'item_type' => $item_type,
			'item_server' => $item_server,
			'group_id'  => $group_id,
		);
		$this->db->where('item_id', $item_id);
		$this->db->update(self::TABLE, $data); 
	}

	public function get_by_name($item_key,$item_server) {
		$query = $this->db->get_where(self::TABLE, array('item_key' => $item_key, 'item_server' => $item_server));
		return $query->row_array();
	}

	public function get_by_id($item_id) {
		$query = $this->db->get_where(self::TABLE, array('item_id' => $item_id));
		return $query->row_array();
	}
	
	public function get_list($group_id) {
		$this->db->order_by('item_sort');
		$query = $this->db->get_where(self::TABLE, array('group_id' => $group_id));
		return $query->result_array();
	}

	public function get_all_item_by_groupid($group_id) {
		$this->db->select('*');
		$this->db->from('report_item');
		$this->db->join('report_group', 'report_group.group_id = report_item.group_id');
		$this->db->where('report_group.group_id', $group_id);
		$query = $this->db->get();

		return $query->result_array();
	}

	public function get_all_item() {
		$this->db->join('report_group', 'report_group.group_id = report_item.group_id');
		$this->db->order_by("group_name asc", "item_sort asc"); 
		$query = $this->db->get('report_item');
		return $query->result_array();
	}

	public function get_item_keys($item_type, $item_server) {
		$this->db->select('item_key'); 
		$query = $this->db->get_where(self::TABLE, array('item_type' => $item_type, 'item_server' => $item_server));
		return $query->result_array();
	}

	public function get_ids_by_ikey($item_key) { 
		$this->db->select('item_id');
		$this->db->from('report_item');
		$this->db->where('item_key', $item_key);
		$query = $this->db->get();
		return $query->result_array();
	}

	public function get_id_by_ikey($item_key) { 
		$this->db->select('item_id');
		$this->db->from('report_item');
		$this->db->where('item_key', $item_key);
		$query = $this->db->get();
		return $query->row_array();
	}

	public function get_diff_items($group_id) { 
		$query = $this->db->query("select distinct item_key, item_name, item_type from report_item where group_id=$group_id");
		return $query->result_array();
	}

}